package com.chaoyong.higo.activity;

import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.LogisticsInfoAdapter;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.bean.GetgoodsAllBean;
import com.chaoyong.higo.bean.KuaiDiBean;
import com.chaoyong.higo.fragment.FragGetGoods0;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.V;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * 物流信息
 * 
 * @author zk
 *
 */
public class LogisticsInfoActivity extends BaseActivity {

	private static final String ClassName = LogisticsInfoActivity.class.getName();

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	/** 商品图片 */
	private ImageView logistics_goods_iv;
	/** 快递状态 */
	private TextView logistics_states_tv;
	/** 快递公司 */
	private TextView logistics_name_tv;
	/** 快递单号 */
	private TextView logistics_order_id;
	/** 物流信息 */
	private ListView logistics_info_lv;

	private LogisticsInfoAdapter adapter;

	private KuaiDiBean bean;

	/** 快递接口用的name */
	private String name;// 快递名称
	/** 快递接口用的code */
	private String code;// 快递编号
	private String uri;

	@Override
	public int layoutResID() {
		return R.layout.activity_logistics_info;
	}

	@Override
	public void findView() {
		findTitleIds();

		logistics_goods_iv = V.f(this, R.id.logistics_goods_iv);
		logistics_states_tv = V.f(this, R.id.logistics_states_tv);
		logistics_name_tv = V.f(this, R.id.logistics_name_tv);
		logistics_order_id = V.f(this, R.id.logistics_order_id);
		logistics_info_lv = V.f(this, R.id.logistics_info_lv);

	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("查看物流");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);

		adapter = new LogisticsInfoAdapter(this);
		logistics_info_lv.setAdapter(adapter);

		getIntentData();
	}

	private void getIntentData() {
		name = getIntent().getStringExtra("kd_key");
		code = getIntent().getStringExtra("kd_code");
		uri = getIntent().getStringExtra("uri");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		httpQueryKD(name, code);
	}

	/**
	 * 快递接口
	 * 
	 * @param name
	 * @param code
	 */
	private void httpQueryKD(String name, String code) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject info = new JSONObject();
			info.putOpt("name", name);
			info.putOpt("code", code);

			param.setBodyEntity(new StringEntity(info.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_kdj, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.i(ClassName, arg0.getMessage());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);
				Gson gson = new Gson();
				bean = gson.fromJson(arg0.result, KuaiDiBean.class);
				KuaiDiBean.DataEntity dataEntity = bean.getData();
				String status = bean.getStatus();
				if (status.equals("1")) {
					List<KuaiDiBean.DataEntity.TracesEntity> entity = bean.getData().getTraces();
					if (!EmptyUtil.isEmpty(entity)) {
						adapter.setList(entity);
					}

					setDatas(dataEntity);
					// showToast(entity.size() + "");
				}
			}
		});
	}

	/**
	 * 设置数据，物流状态、时间等
	 * 
	 * @param dataEntity
	 */
	private void setDatas(KuaiDiBean.DataEntity dataEntity) {
		String state = dataEntity.getState();
		String shipperCode = dataEntity.getShipperCode();
		String logisticCode = dataEntity.getLogisticCode();
		if (!EmptyUtil.isEmpty(state)) {
			if (state.equals("2")) {
				logistics_states_tv.setText("在途中");
			} else if (state.equals("3")) {
				logistics_states_tv.setText("签收");
			} else if (state.equals("4")) {
				logistics_states_tv.setText("问题件");
			}
		} else {
			logistics_states_tv.setText("");
		}

		if (!EmptyUtil.isEmpty(shipperCode)) {
			logistics_name_tv.setText(shipperCode);
		} else {
			logistics_name_tv.setText("");
		}

		if (!EmptyUtil.isEmpty(logisticCode)) {
			logistics_order_id.setText(logisticCode);
		} else {
			logistics_order_id.setText("");
		}

		BitmapUtils utils = new BitmapUtils(this);
		utils.display(logistics_goods_iv, uri);
	}

}
